<?php 
namespace Admin\Controller;

class NavigationController extends BaseController{
    
    public function index(){
        
        // 获取导航数据
        $navigations = M('navigation')->select();
        
        $count = M('navigation')->count();
        $this->assign('count',$count);
        
        // 拼接导航地址
        $navs = array();
        foreach ($navigations as $v){
            if (!empty($v['url'])){
                $v['new_url'] = $v['url'];
            }else {
                $v['new_url'] = U('Home/'.$v['controller'].'/'.$v['action'],true,true);
            }
            $navs[] = $v;
        }
        
        // 传递数据
        $this->assign('navigations',$navs);
        
        $this->display();
    }
    
    public function addNavigation(){
        
        if (IS_POST){
            
            $navModel = D('Navigation');
           
            // 验证表单数据
            if (!$navModel->create()){
                $this->ajaxReturn(array('status'=>0,'msg'=>$navModel->getError()));die;
            }
            
            // 添加数据
            if(I('post.id')>0){ // 修改数据
                
                $res = $navModel->save();
            }else { // 添加数据
                
                $res = $navModel->add();
            }
            
            if ($res !== false){
                $this->ajaxReturn(array('status'=>1));
                
            }else {
                $this->ajaxReturn(array('status'=>0,'msg'=>'失败，请重试!'));
            }
        }elseif($_GET['id'] > 0){
            
            // 获取导航数据
            $nav = M('navigation')->find($_GET['id']);
            
            $this->assign('nav',$nav);
            
            $this->display('navigation_add');
        }
        else {
            
            $this->display('navigation_add');
        }
    }
    
    // 删除导航
    public function delete_nav(){
       
        // 获取id值
        $id = I('get.id');
        $id = trim($id,',');
        
        // 删除导航
        $res = M('navigation')->delete($id);
        
        if ($res !== false){
            
            $this->ajaxReturn(array('status'=>1));
        }else {
            
            $this->ajaxReturn(array('status'=>0));
        }
    }
    
    // 是否显示
    public function change_status(){
        
        $id = I('get.id');
        
        // 获取当前的状态
        $status = M('navigation')->where(array('id'=>$id))->getField('is_show');
        
        // 改变状态
        if($status == 1){
            $res = M('navigation')->where(array('id'=>$id))->save(array('is_show'=>0));
            $status = 0;
        }else {
            $res = M('navigation')->where(array('id'=>$id))->save(array('is_show'=>1));
            $status = 1;
        }
        
        // 返回结果
        if ($res !== false){
            $this->ajaxReturn(array('error'=>1,'status'=>$status));
        }else {
            $this->ajaxReturn(array('error'=>0));
        }
        
    }
}

?>